﻿using AutoMapper;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Read.Api.Handler.Tranceabilty
{
    public class ShowTraceAbleCommandHandler : IRequestHandler<GetTraceableCommand, ApiResult>
    {
        private readonly ItraceabilitableRepository _traceabilitableRepository;

        private readonly IMapper _mapper;

        private readonly ILogger<ShowTraceAbleCommandHandler> _logger;

        private readonly IGoodsRepository _goodsRepository;

        public ShowTraceAbleCommandHandler(ItraceabilitableRepository traceabilitableRepository, IMapper mapper, ILogger<ShowTraceAbleCommandHandler> logger, IGoodsRepository goodsRepository)
        {
            _traceabilitableRepository = traceabilitableRepository;
            _mapper = mapper;
            _logger = logger;
            _goodsRepository = goodsRepository;
        }
        public async Task<ApiResult> Handle(GetTraceableCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var traceable = await _traceabilitableRepository.GetAllAsync(x => x.IsDeleted==false);
                
                return ApiResult.Ok(data: traceable);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "ShowTraceAbleCommandHandler");
                return  ApiResult.Fail( "系统错误");
            }
        }
    }
}
public class GetTraceableCommand:IRequest<ApiResult>
{

}
public class GetTraceableCommandReponse
{
    public int TrId { get; set; }
    /// <summary>
    /// 商品外键
    /// </summary>
    public int GoodsId { get; set; }
    /// <summary>
    /// 生产批次
    /// </summary>
    public string Productionbatches { get; set; } = string.Empty;
    /// <summary>
    /// 保质期（天）
    /// </summary>
    public int ShelflifeDay { get; set; }
    /// <summary>
    /// 生产许可证
    /// </summary>
    public string Productionlicense { get; set; } = string.Empty;
    /// <summary>
    /// 生产企业
    /// </summary>
    public string ProductionCompany { get; set; } = string.Empty;
    /// <summary>
    /// 封面图
    /// </summary>
    public string Coverimage { get; set; } = string.Empty;
}